//
//  addGroupGuestController.m
//  QRMaster
//
//  Created by myApple on 15/10/14.
//  Copyright (c) 2015年 myApple. All rights reserved.
//

#import "addGroupGuestController.h"
#import "addGroupGuestView.h"
#import "groupInfoViewController.h"
#import "RequestNetWork.h"
#import "PromptInfoView.h"
#import "GroupGuestLogic.h"
#import <AddressBook/AddressBook.h>
#import <AddressBookUI/AddressBookUI.h>
#import "PromptInfoView.h"
#import "RequestNetWork.h"
#import "GuestModel.h"
#import "GuestManagerViewController.h"
#import "validateInputValue.h"
#import "MBProgressHUD.h"
#import "GroupGuestLogic.h"
#import "AreaChooseController.h"

@interface addGroupGuestController ()<addGroupGuestInfoViewDelegate,ABPeoplePickerNavigationControllerDelegate,UITextFieldDelegate>
{
    UIView * editingView;
    UITextField * telePhoneTextField;
    UITextField * nameTextField;
    UITextField * ceritificateTextField;
    UIButton * manBtn;
    UIButton * womanBtn;
    UIImageView * manView;
    UIImageView * womanView;
    NSString * sexFlag;
    UIButton * roomBtn;
    NSMutableDictionary * saveDict;
    
    UIImageView * editImgView;
    
    UIActivityIndicatorView * _activityIndicator;
    UIView * editBackView; //保存
    
    //类型
    UIButton * mobileBtn;
    UIButton * emailBtn;
    UIButton * mobileHeaderBtn;
    UIButton * groupManagerAccountBtn;
    UILabel * emailHeaderLabel;
    UILabel * areaCodeIdLabel;
    UILabel * areaLabel;
    UIButton * addAddressTelePhoneBtn;
}
@end

@implementation addGroupGuestController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = RGBA(237, 238, 241, 1);
    sexFlag = @"1";
    // Do any additional setup after loading the view.
    [self PrepareGuestView];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    editBackView.hidden = NO;
    
}

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    self.navigationItem.title = NSLocalizedString(@"addGroupGuest", nil);
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    editBackView.hidden = YES;
    
}

-(void)PrepareGuestView
{
    [self prepareHeadView];
    [self prepareEditingView];
    
}

-(void)addGuestInfoViewBackClick
{
    [self BackClick];
}

-(void)BackClick
{
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)prepareHeadView
{
    float ScreenWidth = [UIScreen mainScreen].bounds.size.width;
    float x = ScreenWidth - 60;;
    float y = 2;
    float width = 40;
    float height = 40;
    
    //编辑
    editBackView = [[UIView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    [self.navigationController.navigationBar addSubview:editBackView];
    x = 10;
    y = 10;
    width = 20;
    height = 20;

    editImgView = [[UIImageView alloc]initWithFrame:CGRectMake(x,y,width,height)];
    editImgView.image = [UIImage imageNamed:@"ic_save"];
    editImgView.userInteractionEnabled = YES;
    [editBackView addSubview:editImgView];
    UITapGestureRecognizer * editTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(saveClick)];
    [editBackView addGestureRecognizer:editTap];
}

-(void)prepareEditingView
{
    float ScreenWidth = [UIScreen mainScreen].bounds.size.width;
    float ScreenHeight = [UIScreen mainScreen].bounds.size.height;
    float x = 0;
    float y = 10;
    float width = ScreenWidth;
    float height = ScreenHeight - y;
    
    editingView = [[UIView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    editingView.backgroundColor = RGBA(237, 238, 241, 1);
    [self.view addSubview:editingView];
    
    //类型
    x = 10;
    y = 10;
    width = ScreenWidth - x - 5;
    height = 50;
    UIView * typeView = [[UIView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    typeView.backgroundColor = RGBA(248, 248, 248, 1);
    [self.view addSubview:typeView];
    
    UILabel * typeLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, 20, width, 20)];
    typeLabel.text = NSLocalizedString(@"type", nil);
    typeLabel.font = [UIFont systemFontOfSize:14.0];
    [typeView addSubview:typeLabel];
    
    // mobile
    x = 90;
    y = 0;
    width = 40;
    height = 40;
    UIImageView * mobileView = [[UIImageView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    [typeView addSubview:mobileView];
    mobileView.userInteractionEnabled = YES;
    UITapGestureRecognizer * mobileTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(mobileClick)];
    [mobileView addGestureRecognizer:mobileTap];
    
    x = 10;
    y = 20;
    width = 20;
    height = 20;
    mobileBtn = [[UIButton alloc]initWithFrame:CGRectMake(x, y, width, height)];
    [mobileBtn setBackgroundImage:[UIImage imageNamed:@"36_1"] forState:0];
    [mobileBtn setBackgroundImage:[UIImage imageNamed:@"36_2"] forState:UIControlStateSelected];
    [mobileBtn addTarget:self action:@selector(mobileClick) forControlEvents:UIControlEventTouchUpInside];
    mobileBtn.selected = YES;
    [mobileView addSubview:mobileBtn];
    
    x = 130;
    y = 20;
    width = 60;
    height = 20;
    UILabel * mobileLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
    mobileLabel.text =  NSLocalizedString(@"loginPromt", nil);
    mobileLabel.font = [UIFont systemFontOfSize:14.0];
    [typeView addSubview:mobileLabel];
    
    //email
    x = 190;
    y = 0;
    width = 40;
    height = 40;
    UIImageView * emailImageView = [[UIImageView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    [typeView addSubview:emailImageView];
    emailImageView.userInteractionEnabled = YES;
    UITapGestureRecognizer * emailTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(emailClick)];
    [emailImageView addGestureRecognizer:emailTap];
    
    x = 10;
    y = 20;
    width = 20;
    height = 20;
    emailBtn = [[UIButton alloc]initWithFrame:CGRectMake(x, y, width, height)];
    [emailBtn setBackgroundImage:[UIImage imageNamed:@"36_1"] forState:0];
    [emailBtn setBackgroundImage:[UIImage imageNamed:@"36_2"] forState:UIControlStateSelected];
    [emailBtn addTarget:self action:@selector(emailClick) forControlEvents:UIControlEventTouchUpInside];
    [emailImageView addSubview:emailBtn];
    
    x = 230;
    height = 20;
    y = 20;
    UILabel * emailLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, y, 60, height)];
    emailLabel.text =  NSLocalizedString(@"mineEmail", nil);
    emailLabel.font = [UIFont systemFontOfSize:14.0];
    [typeView addSubview:emailLabel];
    
    
    //手机号码
    x = 10;
    y = typeView.frame.origin.y + typeView.frame.size.height + 5;
    width = ScreenWidth - x - 5;
    height = 50;
    UIView * telePhoneView = [[UIView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    telePhoneView.backgroundColor = RGBA(248, 248, 248, 1);
    [editingView addSubview:telePhoneView];
    
    x = 100;
    y = 10;
    width = telePhoneView.frame.size.width - x ;
    height = 30;
    telePhoneTextField = [[UITextField alloc]initWithFrame:CGRectMake(x, y, width, height)];
    telePhoneTextField.font = [UIFont systemFontOfSize:14.0];
    telePhoneTextField.borderStyle = UITextBorderStyleNone;
    telePhoneTextField.placeholder = NSLocalizedString(@"telePhoneNum", nil);
    [telePhoneTextField becomeFirstResponder];
    [telePhoneTextField setAutocapitalizationType:UITextAutocapitalizationTypeNone];
    telePhoneTextField.keyboardType = UIKeyboardTypeNumberPad;
    [telePhoneView addSubview:telePhoneTextField];
    
    //前面点击的区号按钮
    x = 10;
    y = 5;
    width = 40;
    mobileHeaderBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    mobileHeaderBtn.frame = CGRectMake(x, y, width, height);
    [mobileHeaderBtn addTarget:self action:@selector(chooseArea) forControlEvents:UIControlEventTouchUpInside];
    [telePhoneView addSubview:mobileHeaderBtn];
    
    areaCodeIdLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 5, 60, 15)];
    areaCodeIdLabel.text = [NSString stringWithFormat:@"+%@",[QRSingleton sharedQRSingleton].currentAreaCodeString];
    areaCodeIdLabel.font = [UIFont systemFontOfSize:10.0];
    [mobileHeaderBtn addSubview:areaCodeIdLabel];
    
    areaLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 15, 60, 15)];
    areaLabel.font = [UIFont systemFontOfSize:10.0];
    areaLabel.text = [QRSingleton sharedQRSingleton].currentAreaString;
    [mobileHeaderBtn addSubview:areaLabel];
    
    emailHeaderLabel = [[UILabel alloc]initWithFrame:CGRectMake(mobileHeaderBtn.frame.origin.x, mobileHeaderBtn.frame.origin.y + 5, mobileHeaderBtn.frame.size.width, mobileHeaderBtn.frame.size.height)];
    emailHeaderLabel.text = NSLocalizedString(@"mineEmail", nil);
    emailHeaderLabel.hidden = YES;
    emailHeaderLabel.font = [UIFont systemFontOfSize:14.0];
    [telePhoneView addSubview:emailHeaderLabel];
    
    x = telePhoneView.frame.size.width - 50;
    y = 0;
    width = 50;
    height = 50;
    addAddressTelePhoneBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    addAddressTelePhoneBtn.frame = CGRectMake(x, y, width, height);
    [addAddressTelePhoneBtn setBackgroundColor:RGBA(135, 199, 66, 1)];
    [addAddressTelePhoneBtn setBackgroundImage:nil forState:0];
    [addAddressTelePhoneBtn addTarget:self action:@selector(addTelePhone) forControlEvents:UIControlEventTouchUpInside];
    [telePhoneView addSubview:addAddressTelePhoneBtn];
    
    UIImageView * adderssImgView = [[UIImageView alloc]initWithFrame:CGRectMake(10,10, 30, 30)];
    adderssImgView.image = [UIImage imageNamed:@"ic_txl"];
    [addAddressTelePhoneBtn addSubview:adderssImgView];
    
    //姓名
    x = 10;
    y = telePhoneView.frame.size.height + telePhoneView.frame.origin.y + 5;
    width = ScreenWidth - x - 5;
    height = 50;
    UIView * nameView = [[UIView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    nameView.backgroundColor = RGBA(248, 248, 248, 1);
    [editingView addSubview:nameView];
    
    x = 10;
    y = 15;
    width = 40;
    height = 20;
    UILabel * editUserNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
    editUserNameLabel.text =  NSLocalizedString(@"userName", nil);
    editUserNameLabel.font = [UIFont systemFontOfSize:14.0f];
    [nameView addSubview:editUserNameLabel];
    
    x = 100;
    y = 10;
    width = nameView.frame.size.width - x ;
    height = 30;
    nameTextField = [[UITextField alloc]initWithFrame:CGRectMake(x, y, width, height)];
    nameTextField.font = [UIFont systemFontOfSize:14.0];
    nameTextField.borderStyle = UITextBorderStyleNone;
    [nameView addSubview:nameTextField];
    
    //身份证
    x = 10;
    y = nameView.frame.size.height + nameView.frame.origin.y + 5;
    width = ScreenWidth - x - 5;
    height = 50;
    UIView * certificateView = [[UIView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    certificateView.backgroundColor = RGBA(248, 248, 248, 1);
    [editingView addSubview:certificateView];
    
    x = 10;
    y = 15;
    width = 60;
    height = 20;
    UILabel * certificateTextLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
    certificateTextLabel.text = NSLocalizedString(@"certificate", nil);
    certificateTextLabel.font = [UIFont systemFontOfSize:14.0f];
    [certificateView addSubview:certificateTextLabel];
    
    x = 100;
    y = 10;
    width = nameView.frame.size.width - x ;
    height = 30;
    ceritificateTextField = [[UITextField alloc]initWithFrame:CGRectMake(x, y, width, height)];
    ceritificateTextField.font = [UIFont systemFontOfSize:14.0];
    ceritificateTextField.borderStyle = UITextBorderStyleNone;
    [certificateView addSubview:ceritificateTextField];
    
    //性别
    x = 10;
    y = 5 + certificateView.frame.size.height + certificateView.frame.origin.y;
    width = ScreenWidth - x - 5;
    height = 50;
    UIView * sexView = [[UIView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    sexView.backgroundColor = RGBA(248, 248, 248, 1);
    [editingView addSubview:sexView];
    
    
    x = 10;
    y = 15;
    width = 60;
    height = 20;
    UILabel * editSexLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
    editSexLabel.text = NSLocalizedString(@"sex", nil);
    editSexLabel.font = [UIFont systemFontOfSize:14.0f];
    [sexView addSubview:editSexLabel];
    
    x = 100;
    y = 0;
    width = 40;
    height = 50;
    manView = [[UIImageView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    [sexView addSubview:manView];
    manView.userInteractionEnabled = YES;
    UITapGestureRecognizer * manTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(manClick)];
    [manView addGestureRecognizer:manTap];
    
    x = 100;
    y = 15;
    width = 20;
    height = 20;
    manBtn = [[UIButton alloc]initWithFrame:CGRectMake(x, y, width, height)];
    [manBtn setBackgroundImage:[UIImage imageNamed:@"36_1"] forState:0];
    [manBtn setBackgroundImage:[UIImage imageNamed:@"36_2"] forState:UIControlStateSelected];
    [manBtn addTarget:self action:@selector(manClick) forControlEvents:UIControlEventTouchUpInside];
    manBtn.selected = YES;
    [sexView addSubview:manBtn];
    
    x = 140;
    height = 20;
    UILabel * manLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, y, 40, height)];
    manLabel.text =  NSLocalizedString(@"man", nil);
    manLabel.font = [UIFont systemFontOfSize:14.0f];
    [sexView addSubview:manLabel];
    
    x = 180;
    y = 0;
    width = 40;
    height = 50;
    womanView = [[UIImageView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    [sexView addSubview:womanView];
    womanView.userInteractionEnabled = YES;
    UITapGestureRecognizer * womanTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(womanClick)];
    [womanView addGestureRecognizer:womanTap];
    
    x += 10;
    y += 15;
    width = 20;
    height = 20;
    womanBtn = [[UIButton alloc]initWithFrame:CGRectMake(x, y, width, height)];
    [womanBtn setBackgroundImage:[UIImage imageNamed:@"36_1"] forState:0];
    [womanBtn setBackgroundImage:[UIImage imageNamed:@"36_2"] forState:UIControlStateSelected];
    [womanBtn addTarget:self action:@selector(womanClick) forControlEvents:UIControlEventTouchUpInside];
    [sexView addSubview:womanBtn];
    
    x = 220;
    height = 20;
    UILabel * womanLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, y, 60, height)];
    womanLabel.text =  NSLocalizedString(@"woman", nil);
    womanLabel.font = [UIFont systemFontOfSize:14.0f];
    [sexView addSubview:womanLabel];
    
    UITapGestureRecognizer * guestTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(resignClick)];
    [self.view addGestureRecognizer:guestTap];
}

#pragma mark ----------点击手机号
-(void)mobileClick
{
    [telePhoneTextField resignFirstResponder];
    mobileBtn.selected = !mobileBtn.selected;
    if (mobileBtn.selected) {
        emailBtn.selected = NO;
        mobileHeaderBtn.hidden = NO;
        emailHeaderLabel.hidden = YES;
        telePhoneTextField.keyboardType = UIKeyboardTypeNumberPad;
        telePhoneTextField.placeholder = NSLocalizedString(@"loginPromt", nil);
        addAddressTelePhoneBtn.hidden = NO;
    }
    else
    {
        emailBtn.selected = YES;
        emailHeaderLabel.hidden = NO;
        mobileHeaderBtn.hidden = YES;
        telePhoneTextField.keyboardType = UIKeyboardTypeDefault;
        telePhoneTextField.placeholder = NSLocalizedString(@"mineEmail", nil);
        addAddressTelePhoneBtn.hidden = YES;
    }
}


#pragma mark ----------点击邮箱
-(void)emailClick
{
    [telePhoneTextField resignFirstResponder];
    emailBtn.selected = !emailBtn.selected;
    if (emailBtn.selected) {
        mobileBtn.selected = NO;
        emailHeaderLabel.hidden = NO;
        mobileHeaderBtn.hidden = YES;
        telePhoneTextField.keyboardType = UIKeyboardTypeDefault;
        telePhoneTextField.placeholder = NSLocalizedString(@"mineEmail", nil);
        addAddressTelePhoneBtn.hidden = YES;
    }
    else
    {
        mobileBtn.selected = YES;
        mobileHeaderBtn.hidden = NO;
        emailHeaderLabel.hidden = YES;
        telePhoneTextField.keyboardType = UIKeyboardTypeNumberPad;
        telePhoneTextField.placeholder = NSLocalizedString(@"loginPromt", nil);
        addAddressTelePhoneBtn.hidden = NO;
    }
}


#pragma mark ---------- 改变地区或者国家
-(void)chooseArea
{
    AreaChooseController * acc = [[AreaChooseController alloc]init];
    [self.navigationController pushViewController:acc animated:YES];
    acc.areaCode = ^(NSString * areaCodeString,NSString * areaString)
    {
        areaCodeIdLabel.text = [NSString stringWithFormat:@"+%@",areaCodeString];
        areaLabel.text = areaString;
    };
}

#pragma mark -- 从通讯录添加职员
-(void)addTelePhone
{
    ABPeoplePickerNavigationController * peoplePicker = [[ABPeoplePickerNavigationController alloc]init];
    peoplePicker.peoplePickerDelegate = self;
    [self presentViewController:peoplePicker animated:NO completion:nil];
}

#pragma mark -- ABPeoplePickerNavigationControllerDelegate
-(void)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker didSelectPerson:(ABRecordRef)person
{
    ABMutableMultiValueRef phoneMult = ABRecordCopyValue(person, kABPersonPhoneProperty);
    NSString * fisrtName = (__bridge NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);
    NSString * lastName = (__bridge NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty);
    if (fisrtName == nil) {
        fisrtName = @"";
    }
    if (lastName == nil) {
        lastName = @"";
    }
    nameTextField.text = [NSString stringWithFormat:@"%@%@",lastName,fisrtName];
    
    NSMutableArray *phones = [NSMutableArray arrayWithCapacity:0];
    for (int i = 0; i < ABMultiValueGetCount(phoneMult); i ++) {
        NSString * aphone = (__bridge NSString *)ABMultiValueCopyValueAtIndex(phoneMult, i);
        [phones addObject:aphone];
    }
    if (phones.count > 0) {
        telePhoneTextField.text = [phones objectAtIndex:0];
        telePhoneTextField.text = [telePhoneTextField.text stringByReplacingOccurrencesOfString:@"-" withString:@""];
    }
    
    [peoplePicker dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark -- ABPeoplePickerNavigationControllerDelegate
-(void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker
{
    [peoplePicker dismissViewControllerAnimated:YES completion:nil];
}

//2_0到8_0系统使用的方法
-(BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person
{
    ABMutableMultiValueRef phoneMult = ABRecordCopyValue(person, kABPersonPhoneProperty);
    NSString * fisrtName = (__bridge NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);
    NSString * lastName = (__bridge NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty);
    if (fisrtName == nil) {
        fisrtName = @"";
    }
    if (lastName == nil) {
        lastName = @"";
    }
    nameTextField.text = [NSString stringWithFormat:@"%@%@",lastName,fisrtName];
    
    NSMutableArray *phones = [NSMutableArray arrayWithCapacity:0];
    for (int i = 0; i < ABMultiValueGetCount(phoneMult); i ++) {
        NSString * aphone = (__bridge NSString *)ABMultiValueCopyValueAtIndex(phoneMult, i);
        [phones addObject:aphone];
    }
    if (phones.count > 0) {
        telePhoneTextField.text = [phones objectAtIndex:0];
        telePhoneTextField.text = [telePhoneTextField.text stringByReplacingOccurrencesOfString:@"-" withString:@""];
    }
    
    [peoplePicker dismissViewControllerAnimated:YES completion:nil];
    return NO;
}



-(void)resignClick
{
    [nameTextField resignFirstResponder];
    [telePhoneTextField resignFirstResponder];
    [ceritificateTextField resignFirstResponder];
}

-(void)saveClick
{
    [self resignClick];
    if (telePhoneTextField.text.length == 0) {
        [PromptInfoView showMessage:NSLocalizedString(@"errorAccount", nil) superView:self.view];
        return;
    }
    if (mobileBtn.selected) {
        if (![validateInputValue isPureNumandCharacters:telePhoneTextField.text]) {
            [PromptInfoView showMessage:NSLocalizedString(@"AccountError", nil) superView:self.view];
            return;
        }
    }
    else if(emailBtn.selected)
    {
        if(![validateInputValue validateEmail:telePhoneTextField.text])
        {
            [PromptInfoView showMessage:NSLocalizedString(@"AccountError", nil) superView:self.view];
            return;
        }
    }
    
    if (nameTextField.text.length < 1 || nameTextField.text.length > 20) {
        [PromptInfoView showMessage:NSLocalizedString(@"userNameLength", nil) superView:self.view];
        return;
    }
    
    NSString * locaString = NSLocalizedString(@"sureAdd", nil);
    NSString * telePhoneString = [NSString stringWithFormat:@"%@%@？",locaString,telePhoneTextField.text];
    UIAlertView * alert = [[UIAlertView alloc]initWithTitle:nil message:telePhoneString delegate:self cancelButtonTitle:NSLocalizedString(@"alterCancel", nil) otherButtonTitles:NSLocalizedString(@"alterSure", nil), nil];
    [alert show];
}


#pragma mark -- alertViewDelegate
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1) {
        [self saveGuestInfo];
    }
}

-(void)saveGuestInfo
{
//    [MBProgressHUD showHUDAddedTo:self animated:YES];
    [self startWaiting:nil color:nil addToView:self.view];
    NSUserDefaults * userInfo = [NSUserDefaults standardUserDefaults];
    __block NSMutableDictionary * postDict = [[NSMutableDictionary alloc]init];
    [postDict setValue:[userInfo objectForKey:@"uid"] forKey:@"id"];
    [postDict setValue:[userInfo objectForKey:@"token"] forKey:@"token"];
    [postDict setValue:_communityId forKey:@"communityId"];
    [postDict setValue:_groupId forKey:@"groupId"];
    if (mobileBtn.selected) {
       
        [postDict setValue:[areaCodeIdLabel.text substringFromIndex:1] forKey:@"areaCode"];
    }
    [postDict setValue:telePhoneTextField.text  forKey:@"account"];
    [postDict setValue:nameTextField.text forKey:@"name"];
    [postDict setValue:ceritificateTextField.text forKey:@"identity"];
    [postDict setValue:sexFlag forKey:@"gender"];
    if([self getTimestamp:_groupId] != nil)
    {
        [postDict setValue:[self getTimestamp:_groupId] forKey:@"timestamp"];
    }
    else
    {
        [postDict setValue:@"" forKey:@"timestamp"];
    }
    __weak addGroupGuestController * weakSelf = self;
    RequestNetWork * rnw = [[RequestNetWork alloc]init];
    [rnw initWithPath:POST(ADDGROUPCLIENT) params:postDict block:^(id downLoadData, id error)
     {
         if ([downLoadData objectForKey:@"cache"] ) {
             if ([[[downLoadData objectForKey:@"cache"] objectForKey:@"status"]integerValue] == 0)
             {
                 [weakSelf updateTimestamp:_groupId timeKeyValue:[[downLoadData objectForKey:@"cache"] objectForKey:@"timestamp"]];
             }
             GuestModel * model = [[GuestModel alloc]init];
             NSMutableDictionary *data = [[NSMutableDictionary alloc]init];
             data = [downLoadData objectForKey:@"data"];
             model.email = [data objectForKey:@"email"];
             model.firstChar = [data objectForKey:@"firstchar"];
             model.sex = [data objectForKey:@"gender"];
             model.headUrl = [data objectForKey:@"headurl"];
             model.guestid = [data objectForKey:@"id"];
             model.certificaties = [data objectForKey:@"identity"];
             model.telePhone = [data objectForKey:@"mobile"];
             model.guestName= [data objectForKey:@"name"];
             model.isGroupManager = @"0";
             if (mobileBtn.selected) {
                  model.areaCode = [areaCodeIdLabel.text substringFromIndex:1];
             }
             else
             {
                 model.areaCode = nil;
             }
             NSMutableArray * guestArray = [[NSMutableArray alloc]init];
             [guestArray addObject:model];
             
             GroupGuestLogic *groupGuest = [[GroupGuestLogic alloc]init];
             [groupGuest insertGroupGuestInfo:model groupName:_groupName groupStartTime:nil groupEndTime:nil groupId:_groupId];
             [weakSelf BackClick];
         }
     if (error)
     {
         if ([[error objectForKey:@"status"]integerValue] == 215) {
             [PromptInfoView showMessage:[error objectForKey:@"info"] superView:self.view];
         }
         else if ([[error objectForKey:@"status"]integerValue] == 208)
         {
             [PromptInfoView showMessage:[error objectForKey:@"info"] superView:self.view];
         }
         else
             [PromptInfoView showMessage:[error objectForKey:@"info"] superView:self.view];
     }
     //         [MBProgressHUD hideHUDForView:self animated:YES];
     [weakSelf stopWaiting];
     }
     ];
}


-(void)womanClick
{
    if (!womanBtn.enabled) {
        return;
    }
    if (womanBtn.selected) {
        manBtn.selected = YES;
        womanBtn.selected = NO;
        sexFlag = @"1";
        
    }
    else
    {
        manBtn.selected = NO;
        womanBtn.selected = YES;
        sexFlag = @"0";
    }
}

-(void)manClick
{
    if (!manBtn.enabled) {
        return;
    }
    if (manBtn.selected) {
        manBtn.selected = NO;
        womanBtn.selected = YES;
        sexFlag = @"0";
    }
    else
    {
        manBtn.selected = YES;
        womanBtn.selected = NO;
        sexFlag = @"1";
    }
}



-(void)hiddenKeyboard
{
    [self resignClick];
}

-(void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}

//通过groupId来查询到时间戳
-(NSString *)getTimestamp:(NSString *)groupId
{
    GroupGuestLogic *groupGuestLogic = [[GroupGuestLogic alloc]init];
    return [groupGuestLogic queryGroupGuestTime:groupId];
}


//通过groupId来更新时间戳
-(void)updateTimestamp:(NSString *)groupId timeKeyValue:(NSString *)timeKeyValue
{
    GroupGuestLogic *groupGuestLogic = [[GroupGuestLogic alloc]init];
    [groupGuestLogic updateGroupGuestTime:groupId keyValue:timeKeyValue];
}


@end
